package com.mantru.iotpot.Adapter

import android.content.Context
import android.widget.ImageView
import android.widget.TextView
import com.mantru.iotpot.Adapter.Base.AppAdapter
import com.mantru.iotpot.Adapter.Base.AppHolder
import com.mantru.iotpot.Bean.AddStepDialogBean
import com.mantru.iotpot.R
import androidx.core.graphics.toColorInt

class AddStepDialogAdapter(var context: Context, layoutResId: Int, var data: MutableList<AddStepDialogBean>): AppAdapter<AddStepDialogBean>(context, layoutResId, data) {

    private var addStepListener: AddStepListener? = null
    fun setAddStepListener(addStepListener: AddStepListener?) {
        this.addStepListener = addStepListener
    }
    interface AddStepListener {
        fun onChoose(position: Int, isSelected: Boolean)
    }

    override fun convert(holder: AppHolder?, position: Int, item: AddStepDialogBean) {
        val textView: TextView? = holder?.getView<TextView>(R.id.adapter_add_step_text)
        val imageView: ImageView? = holder?.getView<ImageView>(R.id.adapter_add_step_background)
        holder?.setText(R.id.adapter_add_step_text, item.num)
            ?.setOnClickListener(R.id.adapter_add_step){
                if ( !item.isSelected ){
                    for (i in 0 until data.size) {
                        data[i].isSelected = false
                    }
                }
                // 切换选择状态
                item.isSelected = !item.isSelected
                addStepListener!!.onChoose(position, item.isSelected)
            }
        // 根据选择状态设置背景
        if (item.isSelected) {
            imageView!!.setBackgroundResource(R.drawable.round_1151e4)
            textView!!.setTextColor("#FFFFFF".toColorInt())
        } else {
            imageView!!.setBackgroundResource(R.drawable.round_line_a1a1a7)
            textView!!.setTextColor("#000000".toColorInt())
        }
    }
}